//投票结算接口
//拼团首页
exports.voteFinish  = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};
    var RewardData = [];
    var joinUserData = [];
    var oid = 0;
    //任务流创建
    async.waterfall([
            //查询机构id
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationActivity + "/oa_id/"+restfulParams.oaid,
                        method: 'POST',
                        data: {
                            fields: ["oa_oid"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        //console.log(data.data);
                        oid = data.data[0].oa_oid;
                        cb();
                    }
                );

            },
            //查询活动的奖励信息
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchVoteActivityReward + "/vr_oaid/"+restfulParams.oaid,
                        method: 'POST',
                        data: {
                            fields: [ "vr_rank","vr_ranknum","vr_coupon_id","vr_coupon_type"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        RewardData = data.data;
                        cb();
                    }
                );

            },
            //获取投票参与者的信息，并且按照得票数倒叙
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_vote_user + "/vau_oaid/"+restfulParams.oaid+"/sort_order/vau_poll-desc",
                        method: 'POST',
                        data: {
                            fields: [ "vau_uid","vau_poll"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        joinUserData = data.data;
                        cb();
                    }
                );
            },
            function (cb){
                for(var i = 0;i<RewardData.length;i++){
                    for(var j = 1;j<=joinUserData.length;j++){
                        if(j>=RewardData[i].vr_rank && j<=RewardData[i].vr_ranknum){
                            var coupon = RewardData[i].vr_coupon_id.split("|");
                            var type = RewardData[i].vr_coupon_type.split("|");
                            for(var k = 0;k<coupon.length;k++){
                                if(coupon[k]!= 0){
                                    if(type[k] == 0){
                                        //进的代金券
                                        R.SEND_HTTP(
                                            req,
                                            {
                                                url: CORE_APIConfig.coreAPI.addUserCashCoupon,
                                                method: 'POST',
                                                data: {
                                                    ucc_uid:joinUserData[j-1].vau_uid,
                                                    ucc_ccid:coupon[k],
                                                    ucc_oid:oid,
                                                }
                                            },
                                            function (err, data) {
                                                //判断是否有错误信息
                                                if (err) {
                                                    return cb(err, data);
                                                }
                                                resultData.addData = data;
                                                cb();
                                            }
                                        );
                                    }else if(type[k] == 2){
                                        //进的兑换券
                                        R.SEND_HTTP(
                                            req,
                                            {
                                                url: CORE_APIConfig.coreAPI.add_exchange_coupon,
                                                method: 'POST',
                                                data: {
                                                    uec_uid:joinUserData[j-1].vau_uid,
                                                    uec_ecid:coupon[k],
                                                    uec_oid:oid,
                                                }
                                            },
                                            function (err, data) {
                                                //判断是否有错误信息
                                                if (err) {
                                                    return cb(err, data);
                                                }
                                                resultData.addData = data;
                                                cb();
                                            }
                                        );
                                    }
                                }
                            }
                        }
                    }
                }

            }


        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}